package org.com.system.util.sql;

import org.apache.commons.lang3.StringUtils;
import org.com.system.util.model.ActionValues;
import org.com.system.util.utils.StrUtils;

public class SaveSQLGener {
	
    /**
     * 生成保存数据SQL,?符格式
     * 例：insert into table_name (column_a,column_b,column_c) values(?,?,?)
     * @param model
     * @return
     */
    public static String saveSQL(Class<?> model){
    	String tableName=SQLUtils.getTableName(model);
    	StringBuffer stber=new StringBuffer("\tINSERT INTO "+tableName+" \n\t\t("+StringUtils.join(SQLUtils.getColumnNames(model),",")+") \n\tVALUES("+StringUtils.join(SQLUtils.getColumnParams(model,SQLUtils.INSERT_OPERATOR,false),",")+")");
    	return StrUtils.out(stber.toString());
    }
    
    /**
     * 生成保存数据SQL,参数格式
     * 例：insert into table_name (column_a,column_b,column_c) values(:column_a,:column_b,:column_c)
     * @param model
     * @return
     */
    public static String saveParamsSQL(Class<?> model){
    	String tableName=SQLUtils.getTableName(model);
    	StringBuffer stber=new StringBuffer("\tINSERT INTO "+tableName+" \n\t\t("+StringUtils.join(SQLUtils.getColumnNames(model),",")+") \n\tVALUES("+StringUtils.join(SQLUtils.getColumnParams(model,SQLUtils.INSERT_OPERATOR,true),",")+")");
    	return StrUtils.out(stber.toString());
    }
    
    /**
     * 生成保存数据SQL,参数格式
     * 例：insert into table_name (column_a,column_b,column_c) values(column_a_value,column_b_value,column_c_value)
     * @param model
     * @return
     */
    public static String saveValuesSQL(ActionValues<String,Object> values,Class<?> model){
    	String tableName=SQLUtils.getTableName(model);
    	StringBuffer stber=new StringBuffer("\tINSERT INTO "+tableName+" \n\t\t("+StringUtils.join(SQLUtils.getColumnNames(model),",")+") \n\tVALUES("+StringUtils.join(values.getBeanValues(model),",")+")");
    	return StrUtils.out(stber.toString());
    }
    
}
